package com.heking.securityapp.pages;

import android.os.Build;
import android.os.Bundle;
import android.os.Environment;
import android.widget.Button;

import com.heking.security.FileUtils;
import com.heking.security.LogUtils;
import com.heking.securityapp.R;

import java.io.File;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

/**
 * @Author 35574
 * @Date 2021/11/16
 * @Description
 */
public class FileManagerActivity extends AppCompatActivity {
    private static final String TAG = ">>>FileManagerActivity";

    private Button mBtn1;
    private Button mBtn2;
    private Button mBtn3;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.file_manager_activity);
        findViews();


        mBtn1.setOnClickListener(v->{
           new ForeachFolder().foreach(file -> {
               if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                   LogUtils.d(TAG, file.getAbsolutePath()+":"+ FileUtils.getName(file));
               }
           });

        });

        mBtn2.setOnClickListener(v->{
            new ForeachFolder().foreach(file -> {
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                    LogUtils.d(TAG, file.getAbsolutePath()+":"+ FileUtils.getNameWithOutSuffix(file));
                }
            });

        });

        mBtn3.setOnClickListener(v->{
            new ForeachFolder().foreach(file -> {
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                    LogUtils.d(TAG, file.getAbsolutePath()+":"+ FileUtils.getSuffix(file));
                }
            });

        });

        LogUtils.d(TAG, "getExternalRoot:"+ FileUtils.getExternalRoot());
        LogUtils.d(TAG, "getRootPath:"+ FileUtils.getRootDir(this));

    }



    private static class ForeachFolder{

        public void foreach(Callback callback){
            File documents = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOCUMENTS);
            if(documents!=null){
                File[] files = documents.listFiles();
                if(files!=null && files.length>0){
                    for (File item : files) {
                        callback.onCallback(item);
                    }
                }
            }
        }

        private  interface Callback{
            void onCallback(File file);
        }
    }



    private void findViews() {

        mBtn1 = findViewById(R.id.btn1);
        mBtn2 = findViewById(R.id.btn2);
        mBtn3 = findViewById(R.id.btn3);
    }




}
